<?php
namespace app\admin\controller;

use think\Exception;

class Video extends Common
{
    private $obj;
    public function _initialize()
    {
        parent::_initialize();
        $this->obj = model('zb_video');
    }

    public function index()
    {
        $where = [];
        if (input('?param.name') && !empty(input('param.name'))) {
            $where['name'] = ['like', '%' . input('param.name') . '%'];
        }
        $data = $this->obj->where($where)->order('id desc')->paginate('25');
        $this->assign('data', $data);
        return view();
    }

    public function video_save() {
        $id = input('param.id', 0);
        if (request()->isPost()) {
            /**
             * 组装数据
             */
            $data['name'] = input('post.name', '');
            $data['url'] = input('post.url', '');
            $data['image'] = input('post.image', '');
            $data['duration'] = input('post.duration', '');

            /**
             * 保存数据
             */
            $where = $id > 0 ? ['id' => $id] : [];
            $rs = $this->obj->save($data, $where);
            if ($rs) {
                $this->success('保存成功');
            } else {
                $this->success('保存失败');
            }
            return true;
        } else {
            $data = [];
            if ($id > 0) {
                $data = $this->obj->get(['id' => $id]);
            }
            $this->assign('data', $data);

            return view();
        }
    }
}
